home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / SDKs / Word Services SDK 1.0.6 / Writeswell Jr 1.2.1 Sources ƒ / Writeswell Jr. Source / Scroll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-15  |  8.9 KB  |  381 lines  |  [TEXT/KAHL]

  1. /* Scroll.c
  2.  * Handle window scrolling in Writeswell, Jr.
  3.  * ©1992 Working Software, Inc.
  4.  * This source code is copyrighted.  Permission is granted to use the Word Services
  5.  * portion of the Writeswell Jr. source code in your own programs, but you 
  6.  * may not distribute the Writeswell Jr. word-processor code as a 
  7.  * commercial product.  If you modify the code, please do not call it 
  8.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  9.  * program and don’t have to deal with a number of different versions with 
  10.  * who-knows-what going on in the code.
  11.  * 
  12.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  13.  * 18 Apr 92 Mike Crawford
  14.  */
  15.  
  16. #include <EPPC.h>
  17. #include <AppleEvents.h>
  18. #include <AEObjects.h>
  19. #include "AERegistry.h"
  20. #include "TBConstants.h"
  21. #include "TBGlobals.h"
  22. #include "Scroll.h"
  23. #include "TTPictures.h"
  24.  
  25. void DoControl( WindowPtr theWindow, ControlHandle ctlHdl, short partCode, Point where )
  26. {
  27.     short    oldVal;
  28.     short    newVal;
  29.     ControlActionUPP    trackProcUPP;
  30.  
  31.     gScrollWindow = theWindow;
  32.  
  33.     switch ( partCode ){
  34.         case inUpButton:
  35.         case inDownButton:
  36.         case inPageUp:
  37.         case inPageDown:
  38.             
  39.             trackProcUPP = NewControlActionProc( TrackScrollBar );
  40.             if ( !trackProcUPP )
  41.                 return;                    // STUB Should report an error
  42.  
  43.             TrackControl( ctlHdl, where, trackProcUPP );
  44.             
  45. #ifdef GENERATINGCFM
  46.             DisposeRoutineDescriptor( trackProcUPP );
  47. #endif
  48.  
  49.             break;
  50.         case inThumb:
  51.             oldVal = GetCtlValue( ctlHdl );
  52.             TrackControl( ctlHdl, where, (ControlActionUPP)NULL );
  53.             newVal = GetCtlValue( ctlHdl );
  54.             
  55.             if ( newVal != oldVal ){
  56.                 ScrollText( oldVal, newVal, ctlHdl );
  57.             }
  58.  
  59.             break;    
  60.     }
  61.  
  62.     return;
  63. }
  64.  
  65. pascal void TrackScrollBar( ControlHandle ctlHdl, short partCode )
  66. {
  67.     short        max;
  68.     short        min;
  69.     short        val;
  70.     short        newVal;
  71.     
  72.     max = GetCtlMax( ctlHdl );
  73.     min = GetCtlMin( ctlHdl );
  74.     val = GetCtlValue( ctlHdl );
  75.     newVal = val;
  76.  
  77.     switch ( partCode ){
  78.         case inUpButton:
  79.             if ( val > min )
  80.                 newVal = val - 1;
  81.             break;
  82.         case inDownButton:
  83.             if ( val < max )
  84.                 newVal = val + 1;
  85.             break;
  86.         case inPageUp:
  87.             newVal = val - gLinesPerPage;
  88.             
  89.             if ( newVal < min )
  90.                 newVal = min;
  91.             break;
  92.  
  93.         case inPageDown:
  94.             newVal = val + gLinesPerPage;
  95.             
  96.             if ( newVal > max )
  97.                 newVal = max;            
  98.             break;
  99.     }
  100.     
  101.     if ( newVal != val ){
  102.         ScrollText( val, newVal, ctlHdl );
  103.     }
  104.  
  105.     return;
  106. }
  107.  
  108. void ScrollText( short oldVal, short newVal, ControlHandle ctlHdl )
  109. {
  110.     TEHandle    textH;
  111.     short        dV;
  112.  
  113.     SetCtlValue( ctlHdl, newVal );
  114.     
  115.     textH = (TEHandle)GetWRefCon( gScrollWindow );
  116.  
  117.     /* 1.0d15 Styled TextEdit requires that we use TEGetHeight to get the height
  118.      * of the actual line, since it is variable.  Under STE, lineHeight is -1.
  119.      *
  120.      * Also, TEGetHeight includes the height of both lines... If we are measuring
  121.      * the height of line 2, we want to do TEGetHeight( 2, 2, textH ).  We need to
  122.      * check for cases of going up and going down.  If we are at the top, though,
  123.      * we want to do TEGetHeight( 0, 1, textH )
  124.      */
  125.     
  126.     if ( newVal > oldVal ){
  127.         /* We're scrolling towards the bottom of the text */
  128.         
  129.         if ( oldVal != 0 ){
  130.             newVal--;
  131.         }
  132.         dV = -TEGetHeight( oldVal, newVal, textH );
  133.     }else{
  134.         /* We're scrolling towards the top of the text */
  135.         /* STUB not sure if I gotta check if we're at the bottom */        
  136.  
  137.         if ( oldVal != 0 ){
  138.             newVal++;
  139.         }
  140.         dV = TEGetHeight( oldVal, newVal, textH );
  141.     }
  142.  
  143. #ifdef NEVER    
  144.     dV = ( oldVal - newVal ) * (*textH)->lineHeight;
  145. #endif
  146.     
  147.     TEScroll( 0, dV, textH );
  148.     
  149.     ScrollPictures( dV, textH );
  150.  
  151.     return;
  152. }
  153.  
  154. Boolean TrackContentClick( void )
  155. {
  156.     Point        where;
  157.     Rect        viewRect;
  158.     TEHandle    textH;
  159.     ControlHandle    ctlHdl;
  160.     GrafPtr            curPort;
  161.     RgnHandle        oldClip;
  162.     Rect            scrollRect;
  163.     Boolean            needRedraw;
  164.     
  165.     /* Track the mouse while it is clicked in the window, and scroll the textEdit
  166.      * field if necessary.  We must always return true.  We have to set the port
  167.      * so that GetMouse will have the right window's local coordinates.
  168.      */
  169.  
  170.     GetPort( &curPort );
  171.     SetPort( gScrollWindow );
  172.  
  173.     GetMouse( &where );
  174.  
  175.     textH = (TEHandle)GetWRefCon( gScrollWindow );
  176.     
  177.     viewRect = (*textH)->viewRect;
  178.     
  179.     ctlHdl = ((WindowPeek)gScrollWindow)->controlList;        /* We know there's 1 control */
  180.  
  181.     needRedraw = false;
  182.  
  183.     /* If the mouse is above or below the window, we fake a mouse click on the scroll bar */
  184.     if ( where.v < viewRect.top ){
  185.         TrackScrollBar( ctlHdl, inUpButton );
  186.         needRedraw = true;
  187.     } else if ( where.v > viewRect.bottom ){
  188.         TrackScrollBar( ctlHdl, inDownButton );
  189.         needRedraw = true;
  190.     }
  191.     
  192.     /* We need to redraw the control so that the user can see the thumb move.
  193.      * This involved setting the clip region to include scroll bar.
  194.      */
  195.  
  196.     if ( needRedraw ){
  197.         oldClip = NewRgn();
  198.         if ( oldClip ){
  199.             GetClip( oldClip );
  200.             scrollRect = (*ctlHdl)->contrlRect;
  201.             ClipRect( &scrollRect );
  202.             Draw1Control( ctlHdl );
  203.             SetClip( oldClip );
  204.             DisposeRgn( oldClip );
  205.         }
  206.     }
  207.  
  208.     SetPort( curPort );
  209.  
  210.     return true;
  211. }
  212.  
  213. void SizeVertScroll()
  214. {
  215. //    MoveControl( gVertScroll, (short) (&qd.thePort->portRect.right - 15), -1 );
  216. //    SizeControl( gVertScroll, 16, (short) (&qd.thePort->portRect.bottom - &qd.thePort->portRect.top - 13) );
  217.  
  218.     MoveControl( gVertScroll, (short) ( qd.thePort->portRect.right - 15), -1 );
  219.     SizeControl( gVertScroll, 16, (short) ( qd.thePort->portRect.bottom -  qd.thePort->portRect.top - 13) );
  220.  
  221.     return;
  222. }
  223.  
  224. void SetVertScroll( WindowPtr theWindow, ControlHandle scrollHdl )
  225. {
  226.     short        totalHeight;
  227.     short        viewHeight;
  228.     TEHandle    hTE;
  229.     Rect        viewRect;
  230.     Rect        destRect;
  231.     short        nLines;
  232.     short        tweenHeight;
  233.     short        i;
  234.     short        scrollAmt;
  235.     short        offSet;
  236.  
  237.     /* The limits of the scroll bar range from 0 to the first line of the last
  238.      * "windowfull" of text.  That is, if there are 50 lines of text, and 10 lines
  239.      * would fit in the window, then the limits are 0 through 40.  If the window is
  240.      * resized so that 20 lines of text would fit, then the limits are 0 and 30.  This
  241.      * way, when the thumb is all the way at the bottom, the last line of text is at
  242.      * the bottom of the window.
  243.      */
  244.  
  245.     hTE = (TEHandle)GetWRefCon( theWindow );
  246.  
  247.     nLines = (*hTE)->nLines;
  248.  
  249.     totalHeight = TEGetHeight( 0, nLines - 1, hTE );
  250.     
  251.     viewRect = (*hTE)->viewRect;
  252.     destRect = (*hTE)->destRect;
  253.  
  254.     viewHeight = viewRect.bottom - viewRect.top;
  255.  
  256.     /* if the last line of text does not fall at or below the bottom of the window,
  257.      * scroll the text down so it does, but not so much that the top line of text is
  258.      * lower than the top of the window.
  259.      */
  260.     
  261.     offSet = viewRect.top - destRect.top;
  262.  
  263.     if ( totalHeight >= viewHeight && ( totalHeight - offSet < viewRect.bottom ) ){
  264.         scrollAmt = -( totalHeight - offSet - viewRect.bottom );
  265.         scrollAmt -= TEGetHeight( nLines, nLines, hTE );
  266.         TEScroll( 0, scrollAmt, hTE );
  267.         viewRect = (*hTE)->viewRect;
  268.         viewHeight = viewRect.bottom - viewRect.top;
  269.         destRect = (*hTE)->destRect;
  270.     }
  271.     
  272.     if ( totalHeight < viewHeight ){
  273.     
  274.         /* All of the text will fit in the window.  Scroll the text so the first
  275.          * line is at the top, and turn off the scroll bar.
  276.          */
  277.         
  278.         scrollAmt = viewRect.top - (*hTE)->destRect.top;
  279.         TEScroll( 0, scrollAmt, hTE );
  280.         
  281.         HiliteControl( scrollHdl, 255 );
  282.     
  283.         return;
  284.     }
  285.     
  286.     /* The text does not all fit.  Find out how many lines from the bottom would fit */
  287.         
  288.     for ( i = nLines - 1; i > 0; i-- ){
  289.         tweenHeight = TEGetHeight( nLines, i, hTE );
  290.         
  291.         if ( tweenHeight >= viewHeight )
  292.             break;
  293.     }
  294.     
  295.     /* i is now the line number of the first line that would be in the window if
  296.      * we had scrolled all the way to the bottom
  297.      */
  298.     
  299.     SetCtlMax( scrollHdl, i - 1 );
  300.  
  301.     /* Save the number of lines on a page for later use by scroll bar tracker */
  302.     
  303.     gLinesPerPage = nLines - i;
  304.  
  305.     for ( i = 1; i < nLines; i++ ){
  306.         tweenHeight = TEGetHeight( 1, i, hTE );
  307.  
  308.         if ( tweenHeight > viewRect.top - destRect.top )
  309.             break;
  310.     }
  311.     
  312.     /* i is now the line number of the first line in the window 
  313.      */
  314.     
  315.     SetCtlValue( scrollHdl, i );
  316.  
  317.     HiliteControl( scrollHdl, 0 );
  318.  
  319.  
  320.     return;
  321. }
  322.  
  323. void ShowSelection( TEHandle hTE )
  324. {
  325.     short    i;
  326.     short    scrollAmt;
  327.     short    linesInRect;
  328.     short    selStart;
  329.     short    viewHeight;
  330.     Rect    viewRect;
  331.     Rect    destRect;
  332.     short    nLines;
  333.     short    lineHeight;
  334.     short    totalHeight;
  335.     short    offset;
  336.     
  337.     selStart = (*hTE)->selStart;
  338.     
  339.     viewRect = (*hTE)->viewRect;
  340.     destRect = (*hTE)->destRect;
  341.     
  342.     offset = viewRect.top - destRect.top;
  343.     
  344.     nLines = (*hTE)->nLines;
  345.     /*totalHeight = TEGetHeight( 0, nLines - 1, hTE );*/
  346.  
  347.     /* scroll as needed to desired line */
  348.     i= 0;
  349.  
  350.     while( i < nLines && (selStart >= ((*hTE)->lineStarts[i])))
  351.         i++;
  352.     
  353.     viewHeight = viewRect.bottom - viewRect.top;
  354.     
  355.     lineHeight = TEGetHeight( 1, i, hTE );
  356.     
  357.     if ( lineHeight - offset > viewRect.bottom ){
  358.     
  359.         scrollAmt = (lineHeight - viewRect.bottom - offset) + viewHeight / 2;
  360.         
  361.         
  362.     }else if ( lineHeight - offset < viewRect.top ){
  363.  
  364.         /* 1.0.3 MDC I didn't have offset here before - it screwed up
  365.          * when you moved off the top with the cursor keys
  366.          */
  367.  
  368.         scrollAmt = -( (viewRect.top + offset - lineHeight ) + viewHeight / 2 );
  369.         
  370.         if ( scrollAmt < -offset )
  371.             scrollAmt = -offset;            /* Limit scrolling so we don't go past top */
  372.     }else
  373.         return;
  374.  
  375.  
  376.     TEScroll(0,-scrollAmt,hTE);
  377.  
  378.     ScrollPictures( -scrollAmt, hTE );
  379.     
  380.     return;    
  381. }